﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace lcmyjobs.Web.Admin
{
    public partial class checkresume : System.Web.UI.Page
    {
        lcmyjobs.BLL.user_basic bub = new BLL.user_basic();
        DataSet ds;
        int count = 0,wcount=0;
        int id;
        protected void Page_Load(object sender, EventArgs e)
        {
            id = int.Parse(Application["webid"].ToString());
            if (!IsPostBack)
            {
                if (Session["adminName"] != null)
                { 
                    getcout(id);
                    //bindData(); //使用url分页，只需在分页事件处理程序中绑定数据即可，无需在Page_Load中绑定，否则会导致数据被绑定两次
                }
                else
                {
                    Maticsoft.Common.MessageBox.ResponseScript(this, "alert('请先登陆！');top.location.href='adminlogin2.aspx'");
                }
              
            }
           

        }
        protected void getcout(int atid)
        {
            ds = bub.PageUserResume(atid, 1, AspNetPager1.PageSize,out count,out wcount);
            lbl_count.Text = wcount.ToString();
            AspNetPager1.RecordCount = count;
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();

        }
        protected void bind(int atid)
        {
            //if (ViewState["page"] != null && ViewState["page"].ToString() != "")
            //    AspNetPager1.CurrentPageIndex = int.Parse(ViewState["page"].ToString());
            ds = bub.PageUserResume(atid, AspNetPager1.StartRecordIndex, AspNetPager1.PageSize, out count,out wcount);
            lbl_count.Text = wcount.ToString();
            AspNetPager1.RecordCount = count;
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();

        }



        protected void AspNetPager1_PageChanged(object sender, EventArgs e)
        {
            id = int.Parse(Application["webid"].ToString());
            bind(id);
            //ViewState["page"] = AspNetPager1.CurrentPageIndex;

        }

        protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                LinkButton linkbtnDel = (LinkButton)e.Row.FindControl("LinkButton1");
                linkbtnDel.Attributes.Add("onclick", "return confirm(\"你确认要删除吗\")");
                if (e.Row.Cells[5].Text == "未审核")
                {
                    e.Row.Cells[5].ControlStyle.ForeColor =System.Drawing.Color.Red;
                }

            }
        }

        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int ID = (int)GridView1.DataKeys[e.RowIndex].Value;
            bub.Delete(ID);
            bind(id);
        }

        protected void btnCheck_Click(object sender, EventArgs e)
        {
            string idlist = GetSelIDlist();
            if (idlist.Trim().Length == 0)
                return;
            bub.UpdateStatus(idlist,1);
            //if(ViewState["page"]!=null)
            //AspNetPager1.CurrentPageIndex = int.Parse(ViewState["page"].ToString());
            bind(id);
        }
        private string GetSelIDlist()
        {
            string idlist = "";
            bool BxsChkd = false;
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                CheckBox ChkBxItem = (CheckBox)GridView1.Rows[i].FindControl("chku");
                if (ChkBxItem != null && ChkBxItem.Checked)
                {
                    BxsChkd = true;

                    Label lb = this.GridView1.Rows[i].FindControl("lbl_email") as Label;
                    
                    lcmyjobs.BLL.user_basic bad = new BLL.user_basic();
                    DataSet ds = bad.FindIDByEmail(lb.Text);

                    
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        string id = ds.Tables[0].Rows[0]["uid"].ToString();
                        idlist += id + ",";
                    }
                    /*
                    if ((i + 1) < GridView1.Rows.Count)
                    {
                        idlist += ",";
                    }

                    
                    if (!string.IsNullOrEmpty(lb.Text))
                    {
                        idlist += lb.Text + ",";
                    }
                    */
                    /*
                    //#warning 代码生成警告：请检查确认Cells的列索引是否正确
                    if (GridView1.DataKeys[i].Value != null)
                    {
                        idlist += GridView1.DataKeys[i].Value.ToString() + ",";
                    }
                     */
                }
            }
            if (BxsChkd)
            {
                idlist = idlist.Substring(0, idlist.LastIndexOf(","));
            }
            return idlist;
        }

        /// <summary>
        /// 获取Email邮件列表
        /// </summary>
        /// <returns></returns>
        private string GetEmailAddlist()
        {
            string emaillist = "";
            bool BxsChkd = false;
            for (int i = 0; i < this.GridView1.Rows.Count; i++)
            {
                CheckBox ChkBxItem = (CheckBox)this.GridView1.Rows[i].FindControl("chku");
                if (ChkBxItem != null && ChkBxItem.Checked)
                {
                    BxsChkd = true;

                    Label lb = this.GridView1.Rows[i].FindControl("lbl_email") as Label;

                    if (!string.IsNullOrEmpty(lb.Text))
                    {
                        emaillist += lb.Text + ",";
                    }
                }
            }
            if (BxsChkd)
            {
                emaillist = emaillist.Substring(0, emaillist.LastIndexOf(","));
            }
            return emaillist;
        }

        protected void btnNOCheck_Click(object sender, EventArgs e)
        {
            string idlist = GetSelIDlist();
            if (idlist.Trim().Length == 0)
                return;
            bub.UpdateStatus(idlist, 2);
            //if (ViewState["page"] != null)
            //    AspNetPager1.CurrentPageIndex = int.Parse(ViewState["page"].ToString());
            bind(id);

        }

        protected void btnDel_Click(object sender, EventArgs e)
        {
            string idlist = GetSelIDlist();
            if (idlist.Trim().Length == 0)
                return;
            bub.deleteusers(idlist);
            try
            {
                //if (ViewState["page"] != null)
                //    AspNetPager1.CurrentPageIndex = int.Parse(ViewState["page"].ToString());
            }
            catch (Exception ex)
            {
                
               // throw;
            }
            bind(id);
        }

        protected void btnMail_Click(object sender, EventArgs e)
        {
            //string idlist = GetSelIDlist();
            string idlist = GetEmailAddlist();

            if (idlist.Length > 0)
            {
                Session["receiveEmailAdd"] = idlist;
                Session["mailType"] = 0; //个人为0
                Response.Redirect("EmailSend.aspx");

            }
            else
            {
                //Maticsoft.Common.MessageBox.Show(this, "请选择所要发送邮件的单位！");
            }
            /*
            if (idlist.Trim().Length == 0)
                return;
            DataSet ds = bub.GetUserEmails(idlist);
            if (ds.Tables[0].Rows.Count > 0)
            {
                try
                {
                    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                    {
                        myPublic.sendMail(ds.Tables[0].Rows[i][0].ToString(), "就业网", "来吧！`");
                    }
                    Maticsoft.Common.MessageBox.Show(this, "发送成功！");
                }
                catch (Exception ex)
                {
                    Maticsoft.Common.MessageBox.Show(this, "发送失败！");
                }
            }
             * */
        }

        protected void AspNetPager2_PageChanged(object sender, EventArgs e)
        {
            bind(id);
           
        }
    }
}